/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import domain.Expense;
import domain.ExpenseError;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import persistence.ExpensePersistence;

/**
 *
 * @author admin
 */
public class ExpenseBusiness {
    
/**
     * 
     * @param expense The Expense to be added to the database
     * @return An ExpenseError Object  containing details if there is an error
     */
    
    public ExpenseError addExpense(Expense expense) {
        ExpenseError err = new ExpenseError();
        ExpensePersistence expPersistence = new ExpensePersistence();

        if (validateExpense(expense)) {
            if (!expPersistence.addExpense(expense)) {
                err.setDbError(true);
                err.setMessage("Error while accessing the database");
            }
        } else {
            err.setValidationError(true);
            err.setMessage("Please fill all the fields");
        }
        return err;
    }
/**
     * 
     * @param expense expense object to be validated
     * @return true if all the fields are filled
     */
    private boolean validateExpense(Expense expense) {
        int errorCount = 0;
        if (expense.getDate().length() == 0) {
            errorCount++;
        }
        if (expense.getAmount() == 0) {
            errorCount++;
        }
        if (expense.getDescription().length() == 0) {
            errorCount++;
        }
        if (expense.getType().length() == 0) {
            errorCount++;
        }
        if (expense.getPlace().length() == 0) {
            errorCount++;
        }
        if (errorCount == 0) {
            return true;
        } else {
            return false;
        }
    }
/**
     * 
     * @return An arraylist containing all the expenses of the current user
     */
    public ArrayList<Expense> getExpenses() {
        
        ExpensePersistence expensePersistence = new ExpensePersistence();

        ArrayList<Expense> expenseList = new ArrayList<Expense>();
        ResultSet rs = expensePersistence.getTableData();

        if (rs!= null) {
            try {
                while(rs.next()) {
                    Expense expense = new Expense();
                    expense.setTransaction_id(rs.getInt("transaction_id"));
                    expense.setDate(rs.getString("date"));
                    expense.setAmount(rs.getDouble("amount"));
                    expense.setDescription(rs.getString("description"));
                    expense.setType(rs.getString("type"));
                    expense.setPlace(rs.getString("place"));
                    
                    expenseList.add(expense);
                }
                return expenseList;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }else{
            return null;
        }
        

    }
/**
     * 
     * @param transaction_id the transaction id of the transaction to be deleted
     * @return true if successful
     */
    public boolean  deleteExpense(int transaction_id) {
        ExpensePersistence exPersistence = new ExpensePersistence();
        return exPersistence.deleteExpense(transaction_id);
    }

    /**
     * 
     * @param expense the Expense to be updated
     * @return  An ExpenseError object containing the details of the occurred error
     */
    public ExpenseError updateExpense(Expense expense) {
        ExpenseError err = new ExpenseError();
        ExpensePersistence expPersistence = new ExpensePersistence();

        if (validateExpense(expense)) {
            if (!expPersistence.updateExpense(expense)) {
                err.setDbError(true);
                err.setMessage("Error while accessing the database");
            }
        } else {
            err.setValidationError(true);
            err.setMessage("Please fill all the fields");
        }
        return err;
    }
}
